package com.sywb.chuangyebao.view.fragment;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.contract.RecommendContract;

import org.bining.footstone.log.Logger;
import org.bining.footstone.rxjava.rxbus.annotation.Subscribe;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;

/**
 * 首页-推荐
 */
public class RecommendFragment extends BaseRefreshFragment<RecommendContract.Presenter> implements RecommendContract.View {

    private int sourceType;//推荐内容来源  0:首页 1:创业万里行

    private boolean isVisibleToTab = true;// 当前Fragemnt所在的Tab页是否可见
    private boolean isVisibleToUser = false;// 当前Fragemnt是否对用户可见

    /**
     * Fragment 实例
     */
    public static RecommendFragment newInstance(Object... pramars) {
        RecommendFragment fragment = new RecommendFragment();
        fragment.setArguments(getBundle(pramars));
        return fragment;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        super.initView(view, bundle);
        if (bundle == null) {
            sourceType = getArguments().getInt("p0", 0);
        } else {
            sourceType = bundle.getInt("p0", 0);
        }
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.MAINTABCHANGE)})
    public void rxMainTabChange(String data) {
        Logger.e("rxMainTabChange：" + data);
        if ((Integer.parseInt(data) == 0 && sourceType == 0) || (Integer.parseInt(data) == 2 && sourceType == 1)) {
            if (!isVisibleToTab) {// 之前所属Tab未显示
                isVisibleToTab = true;
                if (mPresenter != null) {
                    mPresenter.autoResume();
                }
            }
        } else {
            if (isVisibleToTab) {// 之前所属Tab显示
                isVisibleToTab = false;
                if (mPresenter != null) {
                    mPresenter.autoPause();
                }
            }
        }
    }

    /**
     * 横竖屏切换
     *
     * @param newConfig
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (isVisibleToTab && isVisibleToUser) {
            mPresenter.onConfigurationChanged(newConfig);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Logger.e("setUserVisibleHint：" + isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        if (mPresenter != null) {
            mPresenter.setUserVisibleHint();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10000 && mPresenter != null && data != null) {
            mPresenter.onActivityResult(requestCode, resultCode, data);
        }
    }

    /**
     * Activity 重新进入前台 播放逻辑
     */
    @Override
    public void onResume() {
        Logger.e("onResume");
        if (mPresenter != null) {
            mPresenter.autoResume();
        }
        super.onResume();
    }

    /**
     * Activity 不在前台时 暂停播放
     */
    @Override
    public void onPause() {
        Logger.e("onPause");
        if (mPresenter != null) {
            mPresenter.autoPause();
            mPresenter.playStop();
        }
        super.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt("p0", sourceType);
        super.onSaveInstanceState(outState);
    }

    @Override
    public String getStatisticsIdentity() {
        return sourceType == 0 ? "推荐（首页）" : "推荐（创业万里行）";
    }

    @Override
    public int getSourceType() {
        return sourceType;
    }

    @Override
    public boolean getVisibleToTab() {
        return isVisibleToTab;
    }

    @Override
    public boolean getVisibleToUser() {
        return isVisibleToUser;
    }
}
